package com.ruoyi.web.controller.zbg;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.TokenUtils;
import com.ruoyi.system.domain.Approval;
import com.ruoyi.system.domain.ApprovalHistory;
import com.ruoyi.system.domain.TodoRequest;
import com.ruoyi.system.domain.vo.zbg.User;
import com.ruoyi.system.domain.zbg.ToDoVo;
import com.ruoyi.system.service.ToDoService;
import com.ruoyi.system.domain.PageReturnUtil;
import com.ruoyi.web.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "待办事项管理")
@RequestMapping("/zbg/todo")
@RestController
@Slf4j
public class ToDoController {
    @Autowired
    private ToDoService toDoService;
    @Autowired
    private TokenUtils tokenUtils;
    @ApiOperation(value = "待办事项列表")
    @PostMapping("/list")
    public Result<List<ToDoVo>> list(@RequestHeader("Authorization") String authorization, @RequestBody TodoRequest todoRequest) throws Exception {
        log.info("待办事项列表");
        List<ToDoVo> toDoVoList = new ArrayList<>();
        if (authorization != null && authorization.startsWith("Bearer ")) {
            String token = authorization.substring(7);
            String userDataJson = tokenUtils.getUserDataFromToken(token);
            //将json数据转换为User对象
            if (userDataJson != null) {
                ObjectMapper mapper = new ObjectMapper();
                User user = mapper.readValue(userDataJson, User.class);
                toDoVoList = toDoService.selectList(user,todoRequest);
                //预留的分页处理
//                TableDataInfo tableDataInfo = PageReturnUtil.pageResultHandleNoConvert(toDoVoList);

                return Result.success(toDoVoList);
            } else {
                return Result.failed("token校验失败");
            }
        } else {
            return Result.failed("token为空");
        }



    }
    @ApiOperation(value = "审批")
    @PostMapping("/approval")
    public Result approval(@RequestHeader("Authorization") String authorization, @RequestBody Approval approval) throws Exception {
        log.info("审批");
        if (authorization != null && authorization.startsWith("Bearer ")){
            String token = authorization.substring(7);
            String userDataJson = tokenUtils.getUserDataFromToken(token);
            //将json数据转换为User对象
            if (userDataJson != null) {
                ObjectMapper mapper = new ObjectMapper();
                User user = mapper.readValue(userDataJson, User.class);
                toDoService.approval(user,approval);
                return Result.success();
            } else {
                return Result.failed("用户已退出，请重新登录");
            }
        } else {
            return Result.failed("token为空");
        }

    }
    @ApiOperation(value = "获取审批历史数据")
    @PostMapping("/getApprovalHistory")
    public Result<ApprovalHistory> getApprovalHistory(@RequestHeader("Authorization") String authorization, @RequestBody Approval approval) throws Exception {
        log.info("获取审批历史数据");
      ApprovalHistory approvalHistory =   toDoService.getApprovalHistory(approval);
          return Result.success(approvalHistory);
    }
}
